"""
管理数据库连接
"""
import pymysql.cursors


class DBConnect:
    # 定义创建数据库连接的基本参数
    HOST = "127.0.0.1"
    USER_NAME = "root"
    PASSWORD = "root"
    DATABASE = "test"
    # 定义全局的数据库连接属性
    CONN = None

    def get_connect(self):
        """
        创建数据库连接
        :return: 数据库连接对象
        """

        # 建立数据库连接
        if DBConnect.CONN is None:
            DBConnect.CONN = pymysql.connect(host=DBConnect.HOST, user=DBConnect.USER_NAME, password=DBConnect.PASSWORD,
                                             database=DBConnect.DATABASE,
                                             charset="utf8", cursorclass=pymysql.cursors.Cursor)
            return DBConnect.CONN
        else:
            return DBConnect.CONN

    def close_conn(self):
        """
        关闭数据库连接
        :param conn:
        :return:
        """

        if DBConnect.CONN is not None:
            DBConnect.CONN.close()
